package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Book;
import com.example.demo.mapper.BookMapper;
import com.example.demo.service.BookService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Override
    public IPage<Book> pageQuery(int pageNum, int pageSize) {
        return this.page(new Page<>(pageNum, pageSize));
    }

    @Override
    public IPage<Book> search(String name, String publisher, LocalDate startDate, LocalDate endDate, String author, int pageNum, int pageSize) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            wrapper.like(Book::getName, name);
        }
        if (publisher != null && !publisher.isEmpty()) {
            wrapper.like(Book::getPublisher, publisher);
        }
        if (startDate != null) {
            wrapper.ge(Book::getPublishDate, startDate);
        }
        if (endDate != null) {
            wrapper.le(Book::getPublishDate, endDate);
        }
        if (author != null && !author.isEmpty()) {
            wrapper.like(Book::getAuthor, author);
        }
        return this.page(new Page<>(pageNum, pageSize), wrapper);
    }
} 